<?php
/**
 * ============================================================================
 * @package Forum
 * @author Andy
 * @version 16:15
 * @link https://sjdphp.com
 * ----------------------------------------------------------------------------
 */

namespace app\home\controller;


use app\common\controller\HomeBaseController;
use app\common\lib\CommonClass;
use app\common\model\Ad;
use app\common\model\AdCate;
use app\common\model\Inv;
use app\common\model\Nav;
use app\common\model\Ship;
use app\common\server\SystemLog;
use think\facade\View;

class Index extends HomeBaseController
{
    public function index(){

//        $html = View::display("../template/web/default/" . $this->request->action() . '.html',[
//            'navs' => self::getNavLists()
//        ]);

        ## 获取首页轮播
        $adCate = AdCate::where(array("cate_iden" => 'home_rotation','cate_status' => config("project.mysql_code.table_normal")))->find();
        if(!empty($adCate)){
            $rotations = Ad::where(array("cate_id" => $adCate["cate_id"],'ad_status' => config("project.mysql_code.table_normal")))->order("ad_sort desc")->select()->toArray();
        }else{
            $rotations = array();
        }
        ## 获取个人公众号
        $adCate = AdCate::where(array("cate_iden" => 'wechat_qr','cate_status' => config("project.mysql_code.table_normal")))->find();
        if(!empty($adCate)){
            $qr = Ad::where(array("cate_id" => $adCate["cate_id"],'ad_status' => config("project.mysql_code.table_normal")))->find();
        }else{
            $qr = array();
        }
        ## 获取最新文章
        $adCate = AdCate::where(array("cate_iden" => 'new_inv','cate_status' => config("project.mysql_code.table_normal")))->find();
        if(!empty($adCate)){
            $newInv = Ad::where(array("cate_id" => $adCate["cate_id"],'ad_status' => config("project.mysql_code.table_normal")))->find();
        }else{
            $newInv = array();
        }

        ## 获取友情链接
        $ships = Ship::where(array("ship_status" => config("project.mysql_code.table_normal")))->field("ship_id,ship_name,ship_logo,ship_path")->order("ship_sort desc")->select()->toArray();


        ## 获取最新文章
        $newInvLists = Inv::where(array("inv_status" => config("project.mysql_code.table_normal")))->limit(8)->field("inv_id,inv_title")->select()->toArray();


        return View::fetch("../template/web/default/" . $this->request->action() . '.html',[
            'navs' => self::getNavLists(),
            'type' => 'index',
            'rotations' => $rotations,
            'new_inv' => $newInv,
            'qr' => $qr,
            'ships' => $ships,
            'new_invs' => $newInvLists
        ]);
    }



    /** 获取导航数据
     * @param $type
     * @return array
     */
    private static function getNavLists(){
        $lists = [
            'top_nav'   => [],   ## 获取顶部导航
            'foo_nav'   => [],   ## 获取底部导航
            'left_nav'  => [],   ## 获取左侧导航
            'right_nav' => [],   ## 获取右侧导航
            'quick_nav' => [],   ## 获取快捷导航
            'main_nav'  => []    ## 获取主导航
        ];

        ## 循环获取各个位置的导航
        for($i = 1;$i <= 6;$i++){
            try{
                $navLists = Nav::where(array("nav_status" => config("project.mysql_code.table_normal"),'nav_seat' => $i))->field("nav_id,nav_name,nav_type,nav_path,path_type")->order("nav_sort desc")->select()->toArray();
            }catch(\Exception $e){
                SystemLog::insertLog($e->getMessage());
            }
            switch ($i){
                case 1:
                    $lists["top_nav"] = $navLists;
                    break;
                case 2:
                    $lists["foo_nav"] = $navLists;
                    break;
                case 3:
                    $lists["left_nav"] = $navLists;
                    break;
                case 4:
                    $lists["right_nav"] = $navLists;
                    break;
                case 5:
                    $lists["quick_nav"] = $navLists;
                    break;
                default:
                    $lists["main_nav"] = $navLists;
                    break;
            }

        }

        return $lists;
    }



}